package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/user")
public class UserController {
    private final UserService userService;

    @Resource
    private UserMapper userMapper; // 注入 UserMapper

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        User res=userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, user.getUsername()).eq(User::getPassword, user.getPassword()));
        if(res==null){
            return Result.error(-1,"用户名或密码错误");
        }
        return Result.success();
    }

    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        User res=userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, user.getUsername()));
        if(res!=null){
            return Result.error(500,"用户名重复");
        }
        if(user.getPassword() == null)
        {
            user.setPassword("123456");
        }
        userMapper.insert(user);
        return Result.success();
    }

    // 保存用户信息
    @PostMapping
    public Result<?> save(@RequestBody User user) {
        // 如果密码为空，设置默认密码
        if (user.getPassword() == null) {
            user.setPassword("123456");
        }

        boolean success = userService.saveUser(user);
        if (success) {
            return Result.success("User saved successfully");
        } else {
            return Result.error(500, "User save failed");
        }
    }

    // 更新用户信息
    @PutMapping
    public Result<?> update(@RequestBody User user) {
        userMapper.updateById(user);
        return Result.success();
    }

    // 删除用户信息
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable long id) {
        userMapper.deleteById(id);
        return Result.success();
    }

    // 获取分页数据
    @GetMapping
    public Result<?> findPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "") String search) {

        LambdaQueryWrapper<User> wrapper = Wrappers.<User>lambdaQuery();

        if (StrUtil.isNotBlank(search)) {
            wrapper.like(User::getNickname, search);
        }

        Page<User> page = new Page<>(pageNum, pageSize, true);
        IPage<User> result = userMapper.selectPage(page, wrapper);

        // 使用分页响应返回数据
        return Result.successWithPagination(result.getRecords(), result.getTotal());
    }
    @GetMapping("/usersWithTodoList")
    public Result<?> getUsersWithTodoList() {
        List<User> users = userService.getUsersWithTodoList();
        return Result.success(users);
    }
}
